package info.fqtech.skb.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import info.fqtech.skb.demo.utils.HttpUtils;
import info.fqtech.skb.demo.utils.MerchantConfig;
import info.fqtech.skb.demo.utils.SignUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author YASSER   作者 E-mail:   450153554@qq.com
 * @date: 2018-11-05 18:50
 * @version: 1.0.0
 * @description: 提现控制器
 * @modified: <文件修改说明>
 */
@RestController
@RequestMapping("/withdrawal")
public class WithdrawalController {

    /**
     * 申请提现
     * @return
     */
    @RequestMapping("/applyForWithdrawal")
    public Object applyForWithdrawal(String merSubCode,
                                     double orderAmount,
                                     String customerName,
                                     String customerPhone,
                                     String bankName,
                                     String bankAddress,
                                     String bankCard
                                     ) {
        // 创建订单号，需要商户内唯一，长度6-18位纯数字
        String orderNo = String.valueOf(System.currentTimeMillis());
        // 进行签名校验
        Map<String, String> requestMap = new HashMap<>(10);
        requestMap.put("merCode", MerchantConfig.MER_CODE);
        requestMap.put("merSubCode", merSubCode);
        requestMap.put("orderNo", orderNo);
        requestMap.put("orderAmount", SignUtils.formatAmount(orderAmount));
        requestMap.put("customerName", customerName);
        requestMap.put("customerPhone", customerPhone);
        requestMap.put("bankName", bankName);
        requestMap.put("bankAddress", bankAddress);
        requestMap.put("bankCard", bankCard);
        String sign = SignUtils.getSignOfMD5(requestMap, MerchantConfig.MER_KEY);
        requestMap.put("sign", sign);
        // 发起提现申请
        String responseStr = HttpUtils.doPost(MerchantConfig.WITHDRAWAL_ORDER_GATEWAY, requestMap);
        JSONObject jsonObject = null;
        if(StringUtils.isNotBlank(responseStr)) {
            /**
             * 返回的数据格式
             {
                 "msg": "提现申请已受理",
                 "merCode": "8001",
                 "subMerCode": "8001001",
                 "code": "1000",
                 "orderNo": "1541492456502",
                 "orderAmount": "1.00",
                 "sign": "fe0e7fd1ea89371b3f681e94bc7f26e4",
                 "status": true
             }
             */
            jsonObject = JSON.parseObject(responseStr);
            if(jsonObject.getBoolean("status") && Objects.equals("1000", jsonObject.get("code"))) {
                // 如果status为true 和  code为1000。则表示提现申请成功
                // 处理后续业务逻辑

                // ......

            }else {
                // 表示提现申请


            }
        }
        return jsonObject;
    }

    /**
     * 查询提现订单数据状态
     * @param merSubCode -- 下游商户编号
     * @param orderNo -- 订单号
     * @return
     */
    @RequestMapping("/queryWithdrawalStatus")
    public Object queryWithdrawalStatus(String merSubCode, String orderNo) {
        // 进行签名校验
        Map<String, String> requestMap = new HashMap<>(4);
        requestMap.put("merCode", MerchantConfig.MER_CODE);
        requestMap.put("merSubCode", merSubCode);
        requestMap.put("orderNo", orderNo);
        String sign = SignUtils.getSignOfMD5(requestMap, MerchantConfig.MER_KEY);
        requestMap.put("sign", sign);
        // 发起提现申请
        String responseStr = HttpUtils.doGet(MerchantConfig.QUERY_WITHDRAWAL_ORDER_GATEWAY, requestMap);
        JSONObject jsonObject = null;
        if(StringUtils.isNotBlank(responseStr)) {
            /**
             * 返回的数据格式
             * {
                 "msg": "提现成功",
                 "merCode": "8001",
                 "subMerCode": "8001001",
                 "code": "1024",
                 "orderNo": "1541416974501",
                 "orderAmount": "1.00",
                 "sign": "5e9c7bc296220622fa1b97f963fa955c",
                 "status": true
               }
             */
            jsonObject = JSON.parseObject(responseStr);
            if(jsonObject.getBoolean("status") && Objects.equals("1024", jsonObject.get("code"))) {
                // 如果status为true 和  code为1024。则表示提现申请处理成功
                // 处理后续业务逻辑

                // ......

            }else {
                // 表示提现申请处理中或失败


            }
        }
        return jsonObject;
    }

}
